package com.gray.controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.gray.common.UploadImage;
import com.gray.pojo.User;
import com.gray.service.impl.UserService;
import com.gray.util.CookieUtil;
import com.gray.util.ImageUtil;

@Controller
@RequestMapping(value = "/user")
public class UserController {

	@Autowired
	private UserService userServiceImpl;

	/**
	 * 用户注册
	 * 
	 * @param username
	 * @param password
	 * @param email
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "registerAction")
	public int registerAction(HttpServletRequest request,HttpServletResponse response, @RequestParam(required = true) String username,
			@RequestParam(required = true) String password, @RequestParam(required = true) String email) {
		int i =  userServiceImpl.register(username, password, email, request.getRemoteHost());
		if(i>0){
			CookieUtil.setCookie(response, "userName", username, 30);
			CookieUtil.setCookie(response, "passWord", password, 30);
			System.out.println(i);
		}
		return i;
	}

	/**
	 * 登录
	 * 
	 * @param request
	 * @param username
	 * @param password
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "loginAction")
	public int loginAction(HttpServletRequest request,HttpServletResponse response, @RequestParam(required = true) String username,
			@RequestParam(required = true) String password) {
		User user = userServiceImpl.getUserByNameAndPwd(username, password);

		if (user != null) {
			user.setUserLastLoginIp(request.getRemoteHost());
			userServiceImpl.updataUser(user);
			request.getSession().setAttribute("user", user);
			CookieUtil.setCookie(response, "userName", user.getUserName(), 40);
			CookieUtil.setCookie(response, "passWord", user.getUserPwd(), 40);
			return 200;
		}
		return 500;
	}

	@RequestMapping(value = "goMain")
	public String goMain() {

		return "../../hello";
	}

	/**
	 * 获取留言 TODO
	 * 
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "stayMessage")
	public List stayMessage() {
		List list = userServiceImpl.getStayMessage(1);
		return list;
	}
	
	
	@ResponseBody
	@RequestMapping(value = "getStayMessage")
	public List getStayMessage(Integer limit,Integer offset) {
		List list = userServiceImpl.getStayMessage(1);
		return list;
	}

	/**
	 * 跳转到后台管理
	 * 
	 * @return
	 */
	@RequestMapping(value = "backStag")
	public String backStag() {
		return "system/back-stag-management";
	}

	/**
	 * 更新个人信息
	 * 
	 * @param request
	 * @param userPhone
	 * @param userSex
	 * @param file
	 * @return
	 * @throws IOException
	 */
	@RequestMapping(value = "updateuserinfo")
	@ResponseBody
	public int updataUserInfo(HttpServletRequest request, @RequestParam(required = false) Integer userPhone,
			@RequestParam(required = false) String userSex, @RequestParam(required = false) String userEmail,
			@RequestParam(required = false) String userAddress, @RequestParam(required = false) MultipartFile file // 头像
	) throws IOException {
		User user = (User) request.getSession().getAttribute("user");
		UploadImage uploadimage = null;
		if (userPhone != null) {
			user.setUserPhone(userPhone);
		}
		System.out.println(file.getOriginalFilename());
		if (userAddress != null) {
			user.setUserAddress(userAddress);
		}
		if (userEmail != null) {
			user.setUserEmail(userEmail);
		}
		try {
			if (ImageUtil.isImageFile(file.getOriginalFilename())) {
				System.out.println(file.getOriginalFilename());
				uploadimage = new UploadImage(file.getOriginalFilename(), file.getInputStream());
				if (uploadimage != null) {
					return userServiceImpl.updataUser(user, uploadimage);
				}
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return userServiceImpl.updataUser(user);
	}

	/**
	 * 获取用户信息
	 * 
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "getserinfobysession")
	public Map getUserInfoBySession(HttpServletRequest request) {

		User user = (User) request.getSession().getAttribute("user");
		Map map = new HashMap();
		if (user == null) {
			return map;
		}
		User user2 = userServiceImpl.getUserById(user.getUserId());

		if (user2 != null) {
			map.put("user", user2);
			map.put("sucess", true);
		} else {
			map.put("sucess", false);
		}
		return map;
	}

	/**
	 *  发表留言
	 * @param request
	 * @param message_content
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "reportStayMessage")
	public int reportStayMessage(HttpServletRequest request, @RequestParam(required = true) String message_content) {
		User user = (User) request.getSession().getAttribute("user");
		if (user == null) {
			user = new User();
			user.setUserId(3);
		}

		// todo ROOT账号的留言板
		return userServiceImpl.reportStayMessage(1, user.getUserId(), message_content, request.getRemoteHost());

	}

	@RequestMapping(value = "main1")
	public String main1() {
		return "main1";
	}

	/**
	 * 更新用户密码
	 * 
	 * @param req
	 * @param userPwd
	 * @param oldUserPwd
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "updateUserPWD",method=RequestMethod.POST)
	public Map updateUserPwd(HttpServletRequest req, @RequestParam(required = true) String userPwd,
			@RequestParam(required = true) String oldUserPwd) {
		User user = (User) req.getSession().getAttribute("user");
		Map map = new HashMap();
		System.out.println(oldUserPwd);
		if (user != null && oldUserPwd.equals(user.getUserPwd())) {
			user.setUserPwd(userPwd);
			int i = userServiceImpl.updataUser(user);
			
			if (i > 0) {
				map.put("sucess", true);
			} else {
				map.put("sucess", false);
			}
			return map;
		}
		map.put("info", "密码错误");
		return map;
	}
	
	/**
	 * 批量删除留言
	 * @param request
	 * @param stayIds
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="batchdeletestaymessage")
	public Map batchDeleteStayMessage(HttpServletRequest request,
			@RequestParam(required=true) String stayIds){
		User user = (User)request.getSession().getAttribute("user");

		Map map = new HashMap();
		if(null != user && 1==user.getUserId()){ //只有root账户有该权限
//			int i = userServiceImpl.batchDeleteStayMessage(stayIds);
//			map.put("sucess", i);
			System.out.println(stayIds);
			String temp[] = stayIds.split(",");
			List list = new ArrayList();
			for (String string : temp) {
				short e = Short.parseShort(string);
				list.add(e);
			}
			userServiceImpl.batchDeleteStayMessage(list);
			return map;
		}
		map.put("sucess", false);
		return map;
	}
}
